package com.erp.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.erp.dao.ForumDAO;
import com.erp.dao.TopicDAO;
import com.erp.domain.TForum;
import com.erp.domain.TTopic;
import com.erp.dto.Page;
import com.erp.dto.Topic;
import com.erp.mapping.ArticleType;
import com.erp.mapping.Status;

@Service(value = "topicService")
@Transactional
public class TopicService {

	private TopicDAO topicDAO = null;
	private ForumDAO forumDAO = null;

	public TopicDAO getTopicDAO() {
		return topicDAO;
	}

	@Resource(name = "topicDAO")
	public void setTopicDAO(TopicDAO topicDAO) {
		this.topicDAO = topicDAO;
	}

	public ForumDAO getForumDAO() {
		return forumDAO;
	}

	@Resource(name = "forumDAO")
	public void setForumDAO(ForumDAO forumDAO) {
		this.forumDAO = forumDAO;
	}

	
	public List<Topic> listByforum(Topic topic) {
		List<Topic> topicList = new ArrayList<Topic>();
		List<TTopic> tTopicList = new ArrayList<TTopic>();
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("tForum", forumDAO.getById(topic.getForum().getId()));
		// 置顶帖始终在上面
		tTopicList = topicDAO.list(
				"from TTopic topic where topic.tForum=:tForum and topic.status="
						+ Status.STATUS_NORMAL.ordinal()
						// 前提：置顶类型序号比普通类型大
						+ " order by (case topic.articleType when "
						+ ArticleType.TYPE_TOP.ordinal() + " then "
						+ ArticleType.TYPE_TOP.ordinal() + " else "
						+ ArticleType.TYPE_COMMON.ordinal()
						+ " end) desc,topic.lastUpdateTime desc", mapping);

		if (tTopicList != null) {
			for (TTopic tTopic : tTopicList) {
				topicList.add(new Topic(tTopic));
			}
		}
		return topicList;
	}

	
	public List<Topic> pagedListByforum(Topic topic, Page page) {
		// 开始页码
		int from = (page.getPageIndex() - 1) * page.getPageSize();
		String hql = "from TTopic topic where topic.tForum=:tForum and topic.status="
				+ Status.STATUS_NORMAL.ordinal()
				// 前提：置顶类型序号比普通类型大
				+ " order by (case topic.articleType when "
				+ ArticleType.TYPE_TOP.ordinal()
				+ " then "
				+ ArticleType.TYPE_TOP.ordinal()
				+ " else "
				+ ArticleType.TYPE_COMMON.ordinal()
				+ " end) desc,topic.lastUpdateTime desc";
		List<Topic> topicList = null;
		Map<String, Object> mapping = new HashMap<String, Object>();
		mapping.put("tForum", forumDAO.getById(topic.getForum().getId()));
		int totalRecord = topicDAO.count("select count(*) " + hql, mapping)
				.intValue();
		page.setTotalRecord(totalRecord);
		int totalPage = 1;
		if (totalRecord > 0) {
			topicList = new ArrayList<Topic>();
			List<TTopic> tTopicList = null;
			tTopicList = topicDAO
					.paging(hql, mapping, from, page.getPageSize());
			// 设置总页数
			totalPage = totalRecord % (page.getPageSize()) > 0 ? totalRecord
					/ (page.getPageSize()) + 1
					: totalRecord / (page.getPageSize());

			for (int i = 0; i < tTopicList.size(); i++) {
				topicList.add(new Topic(tTopicList.get(i)));
			}
		}
		page.setTotalPage(totalPage);
		return topicList;
	}

	
	public boolean add(Topic topic) {
		TTopic tTopic = new TTopic(topic);
		TForum tForum = forumDAO.getById(topic.getForum().getId());
		tForum.setTopicCount(tForum.getTopicCount() + 1);
		tTopic.settForum(tForum);
		topicDAO.save(tTopic);
		return true;
	}

	/**
	 * (在回收站里)彻底删除主题
	 * 
	 * @param model
	 */
	public boolean delete(Topic topic) {
		topicDAO.deleteById(topic.getId());
		return true;
	}

	/**
	 * 添加已删除标记，同时将版块显示的主题数量-1
	 * 
	 * @param topic
	 * @return
	 */
	public boolean setStatusDeleted(Topic topic) {
		TTopic tTopic = topicDAO.getById(topic.getId());
		tTopic.setStatus(Status.STATUS_DELETED);// 设为已删除
		TForum tForum = tTopic.gettForum();
		tForum.setTopicCount(tForum.getTopicCount() - 1);// 版块的主题数-1
		return true;
	}

	/**
	 * 将主题 状态 恢复为正常，同时将版块显示的主题数量+1
	 * 
	 * @param forum
	 * @return
	 */
	public boolean setStatusNormal(Topic topic) {
		TTopic tTopic = topicDAO.getById(topic.getId());
		tTopic.setStatus(Status.STATUS_NORMAL);// 恢复为正常状态
		TForum tForum = tTopic.gettForum();
		tForum.setTopicCount(tForum.getTopicCount() + 1);// 版块的主题数+1
		return true;
	}

	/**
	 * 设置主题 类型 为普通帖
	 * 
	 * @param topic
	 * @return
	 */
	public boolean setTypeCommon(Topic topic) {
		topicDAO.getById(topic.getId()).setArticleType(ArticleType.TYPE_COMMON);
		return true;
	}

	/**
	 * 设为精华
	 * 
	 * @param topic
	 * @return
	 */
	public boolean setTypeBest(Topic topic) {
		topicDAO.getById(topic.getId()).setArticleType(ArticleType.TYPE_BEST);
		return true;
	}

	/**
	 * 设为置顶
	 * 
	 * @param topic
	 * @return
	 */
	public boolean setTypeTop(Topic topic) {
		topicDAO.getById(topic.getId()).setArticleType(ArticleType.TYPE_TOP);
		return true;
	}

	
	public Topic getTopicById(Integer topicId) {
		TTopic tTopic = topicDAO.getById(topicId);
		if (tTopic == null) {
			return null;
		} else {
			return new Topic(tTopic);
		}
	}

	/**
	 * 获取主题回收站列表
	 * 
	 * @return
	 */
	public List<Topic> getRecycleList() {
		List<Topic> topicRecycleList = new ArrayList<Topic>();
		List<TTopic> tTopicRecycleList = new ArrayList<TTopic>();
		tTopicRecycleList = topicDAO
				.list("from TTopic topic where topic.status="
						+ Status.STATUS_DELETED.ordinal()
						+ " order by topic.lastUpdateTime desc");

		if (tTopicRecycleList != null) {
			for (TTopic tTopic : tTopicRecycleList) {
				topicRecycleList.add(new Topic(tTopic));
			}
		}
		return topicRecycleList;
	}

}
